package com.sgl.service;

//import com.uni.common.entity.TakerApply;

import com.sgl.dao.UserMapper;
import com.sgl.entity.User;
import com.sgl.support.ThreadLocalUser;
import com.sgl.util.PageBean;
import com.sgl.util.ReturnData;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.jws.soap.SOAPBinding;
import java.util.List;
import java.util.Map;

/**
*	@author yanzheng_Yu
*	@date 2019年1月20日
*/
@Service
public class UserService {
	
	@Autowired
	private UserMapper userMapper;


	public User findById(Long id) {
		return userMapper.findById(id);
	}

	public void testLocal() {
		User user = ThreadLocalUser.getInstance().getUser();
		System.out.println(user.getId());
	}

	public List<User> findByRole(String role){
		return userMapper.findByRole(role);
	}


	@Transactional(rollbackFor=Exception.class)
	public int update(User user) {
		return userMapper.update(user);
	}

//	public ReturnData bindPhone(String code, String encryptedData, String iv) {
//		ReturnData rd = new ReturnData();
//		String result = WechatUtil.getSessionKeyOropenid(code);
//		if(StringUtils.isBlank(result)) {
//			return rd.failBack("系统异常，请重试");
//		}
//		JSONObject wx = JSON.parseObject(result);
//		String sessionKey = wx.getString("session_key");
//
//		User user = ThreadLocalUser.getInstance().getUser();
//
//		JSONObject userJson = WechatUtil.getUserInfo(encryptedData, sessionKey, iv);
//		String phone = userJson.getString("purePhoneNumber");
//
//		User updateUser = new User();
//		updateUser.setId(user.getId());
//		updateUser.setPhone(phone);
//		updateUser.setCountryCode(userJson.getString("countryCode"));
//		updateUser.setUpdateTime(new Date());
//		int count = userMapper.update(updateUser);
//		updateUser.setId(null);
//		if(count > 0) {
//			return rd.okBack(updateUser);
//		}
//		return rd.failBack("失败");
//	}


	@Transactional(rollbackFor=Exception.class)
	public int updateBalance(Map<String, Object> updateUser) {
		int oc = userMapper.updateBalance(updateUser);
		if(oc != 1) {
			//事务回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return 0;
		}
		return oc;
	}

	@Transactional(rollbackFor=Exception.class)
	public void updateDeductionNumber(Long userId, int deductionNumber) {
		userMapper.updateDeductionNumber(userId,deductionNumber);
	}

//	public UserBaseInfoDTO findByBaseInfo(Long userId) {
//		return userMapper.findByBaseInfo(userId);
//	}

	public User findByBgLoginName(String bgLoginName) {
		return userMapper.findByBgLoginName(bgLoginName);
	}

	
	public int insert(User user) {
		return userMapper.insert(user);
	}

	public int updateUserStatus(String[] ids) {
		return userMapper.updateUserStatus(ids);
	}

	public List<User> findList(PageBean<User> pageBean){
		return userMapper.findList(pageBean);
	}

	public int countList(PageBean<User> pageBean){
		return userMapper.countList(pageBean);
	}

	@Transactional
	public ReturnData updateUserRole(User userRoleDTO){

		int rs=userMapper.updateRole(userRoleDTO);
		if (rs>0){
			return new ReturnData().okBack("修改成功");
		}
		return new ReturnData().failBack("当前没有修改任何信息");
	}
}
